<template>
  <div>
    <backHeader pageTitle="频道" />
    <div id='container' class="container" ref="container">
      <ul class="category">
        <li class="topcategory">
          <router-link to="home">
            <h4>全部影片 >></h4>
          </router-link>
        </li>
      </ul>
      <ul class="category" v-for="item in data" :key="item.id">
        <li class="topcategory">
            <h4>{{item.name}}</h4>
        </li>
        <li>
          <span><router-link :to="{path:'home', query:{category_id:item.id}}">全部</router-link></span>
          <span v-for="childItem in item.child" :key="childItem.id">
            <router-link :to="{path:'home', query:{category_id:childItem.id}}">{{childItem.name}}</router-link>
          </span>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import backHeader from "@/components/public/backHeader.vue";
import api from "@/lib/apis/movies.js";
import { size, sizeType } from "@/lib/getSize.js";
export default {
  namr: "category",
  data() {
    return { data: [], device: "" };
  },
  mounted() {
    this.getCategorys();
    this.initLayout();
  },
  methods: {
    getCategorys() {
      //仅支持两层目录，并且保证接口按父节点升序排列.
      api.getCategory().then(response => {
        var result = [];
        var tmp_data = [];
        for (var i = 0; i < response.data.length; ++i) {
          var m_data = { id: "", name: "", child: [] };
          m_data.id = response.data[i].id;
          m_data.name = response.data[i].name;
          if (response.data[i].parent_id == 0) {
            result.push(m_data);
          } else {
            for (var j = 0; j < result.length; ++j) {
              if (result[j].id == response.data[i].parent_id) {
                result[j].child.push(response.data[i]);
              }
            }
          }
        }
        this.data = result;
      });
    },

    /*
      布局相关
    */
    initLayout() {
      //初始化容器.
      this.device = size.getDeviceType();
      let rectSize = size.GET(sizeType.document);
      this.$refs.container.style.width = rectSize.width + "px";
      this.$refs.container.style.height = rectSize.height - 40 + "px";
      this.$refs.container.style.marginTop = "40px";
    }
  },
  components: {
    backHeader
  }
};
</script>

<style>
a {
  text-decoration: none;
  color: #409eff;
}
.container {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
  clear: both;
  background-color: hsl(0, 0%, 96%);
  width: 100%;
  overflow-y: auto;
}
.category {
  position: relative;
  list-style: none;
  width: 100%;
  margin: 0;
  padding: 0;
}
.topcategory {
  margin-top: 5px;
  padding-top: 5px;
}
.category li {
    margin-left: 20px;
  padding-left: 15px;
  padding-right: 15px;
  clear: both;
}
.category span {
  text-align: center;
  margin-right: 15px;
  margin-top: 15px;
  margin-bottom: 15px;
  line-height: 22px;
  font-size: 12px;
  width: 60px;
  color: #409eff;
  cursor: pointer;
}

.category li h4 {
  font-size: 14px;
  line-height: 16px;
  margin-top: 10px;
  margin-bottom: 10px;
  cursor: pointer;
}
</style>